﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using LUStationery.Model;
using LUStationery.DAO;


namespace LUStationery.Controller
{
    public class OrdersControl
    {
        public void addOrder(string supplier, string itemCode, int orderQty)
        {
            ItemListControl ilcont = new ItemListControl();
            Item i = ilcont.getItemByItemCode(itemCode);
            SupplierControl scont = new SupplierControl();
            Supplier s = scont.getSupplierBySupplierName(supplier);
            SupplierItem si = scont.getSupplierItemBySupplierItem(itemCode, supplier);
            Order o = new Order(i, s, si, orderQty);
            OrderDAO oDAO = new OrderDAO();
            oDAO.addOrder(o.SupplierItemID, o.ItemCode, o.ItemDesc, o.SupplierCode, o.SupplierName, o.OrderQty, o.Price, o.OrderPrice, o.OrderDate, o.Status);
        }

        public OrderList getOrdersBySupplierAndStatus(string supplier, string status)
        {
            OrderDAO oDAO = new OrderDAO();
            IDataReader idr = oDAO.getOrdersBySupplierAndStatus(supplier, status);
            return OrdersControl.convertIdrToOrderList(idr);

        }

        public Order getOrderByOrderID(int orderID)
        {
            OrderDAO oDAO = new OrderDAO();
            IDataReader idr = oDAO.getOrderByOrderID(orderID);
            return OrdersControl.convertIdrToOrder(idr);
        }

        public void addToPo(Order o)
        {
            OrderDAO oDAO = new OrderDAO();
            int poID = o.PurchaseOrderID;
            int orderID = o.OrderID;
            oDAO.addOrderToPo(poID, orderID);
        }

        protected static Order convertIdrToOrder(IDataReader idr)
        {
            Order o = new Order();
            while (idr.Read())
            {
                o.OrderID = Convert.ToInt32(idr["order_id"]);
                o.ItemCode = Convert.ToString(idr["item_code"]);
                o.ItemDesc = Convert.ToString(idr["item_description"]);
                o.OrderQty = Convert.ToInt32(idr["order_qty"]);
                o.Price = Convert.ToDouble(idr["price"]);
                o.OrderDate = Convert.ToDateTime(idr["order_date"]);
                o.Status = Convert.ToString(idr["order_status"]);
                string t = Convert.ToString(idr["purchase_order_id"]);
                if (t == "")
                {
                    o.PurchaseOrderID = 0;
                }
                else
                {
                    o.PurchaseOrderID = Convert.ToInt32(idr["purchase_order_id"]);
                }
            }
            return o;
        }
        public void updateOrder(int poID, string status)
        {
            OrderDAO oDAO = new OrderDAO();
            oDAO.updateOrderStatus(poID, "processing");

        }
        protected static OrderList convertIdrToOrderList(IDataReader idr)
        {
            OrderList oList = new OrderList();
            while (idr.Read())
            {
                Order o = new Order();
                o.OrderID = Convert.ToInt32(idr["order_id"]);
                o.ItemCode = Convert.ToString(idr["item_code"]);
                o.ItemDesc = Convert.ToString(idr["item_description"]);
                o.OrderQty = Convert.ToInt32(idr["order_qty"]);
                o.Price = Convert.ToDouble(idr["price"]);
                o.OrderDate = Convert.ToDateTime(idr["order_date"]);
                o.Status = Convert.ToString(idr["order_status"]);
                o.OrderPrice = Convert.ToDouble(idr["order_price"]);
                string t = Convert.ToString(idr["purchase_order_id"]);
                if (t == "")
                {
                    o.PurchaseOrderID = 0;
                }
                else
                {
                    o.PurchaseOrderID = Convert.ToInt32(idr["purchase_order_id"]);
                }
                oList.Add(o);
            }
            return oList;
        }

    }
}
